import { _decorator, Collider, Component, Enum, ITriggerEvent, Node, Tween, tween, v3 } from 'cc';
import { Amplification, AMPLIFICATION, ContentColor, DOOR, Group } from './GameConstant';
import { Label3D } from '../../1/Label3D';
import { Barrier } from './Barrier';
import { GameManager } from './GameManager';
import { PoolManager } from '../AshFramework/Managers/PoolManager';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass, property } = _decorator;

const borderX: number = 0.019;

@ccclass('Door')
export class Door extends Barrier {

    @property({ type: Enum(DOOR) })
    DoorType: DOOR = DOOR.GREEN;

    @property({ type: Enum(AMPLIFICATION) })
    AmplificationType: AMPLIFICATION = AMPLIFICATION.RATE;

    @property(Node)
    Doors: Node[] = [];

    @property(Label3D)
    TitleLabel3D: Label3D = null;

    @property(Label3D)
    CurNumLabel3D: Label3D = null;

    @property(Label3D)
    AmpNumLabel3D: Label3D = null;

    @property
    CurNum: number = 1;

    @property
    AmpNum: number = 1;

    @property
    Places: number = 0;

    @property
    IsMove: boolean = false;

    @property
    Speed: number = 0;

    IsAmplification: boolean = false;
    IsRemove: boolean = false;
    DirX: number = 0;

    protected onLoad(): void {
        super.onLoad();
        this.Collider.on(`onTriggerStay`, this.onTriggerStay, this);
    }

    protected start(): void {
        this.getDir();
        this.TitleLabel3D.string = Amplification[this.AmplificationType];
        let str: string = "";
        if (this.AmpNum < 0) {
            this.AmpNumLabel3D.color = ContentColor.Red;
            str = this.AmpNum.toString();
        } else {
            str = "+" + this.AmpNum.toString();
        }
        this.AmpNumLabel3D.string = str;
        this.showUI();
    }

    protected update(dt: number): void {
        if (this.IsMove) {
            this.updateDir();
            this.node.setPosition(this.node.getPosition().clone().add3f(this.DirX * this.Speed * dt, 0, 0));
        }
    }

    getDir() {
        const rand = Math.random();
        if (rand < 0.5) {
            this.DirX = -1;
        } else {
            this.DirX = 1;
        }
    }

    updateDir() {
        if (this.node.getPosition().x >= borderX) {
            this.DirX = -1;
        } else if (this.node.getPosition().x <= -borderX) {
            this.DirX = 1;
        }
    }

    showUI() {
        if (this.CurNum >= 0) {
            this.DoorType = DOOR.GREEN;
        } else {
            this.DoorType = DOOR.RED;
        }
        this.CurNumLabel3D.string = this.CurNum.toString();
        //展示门的颜色
        this.Doors.map(value => value.active = false);
        this.Doors[this.DoorType].active = true;
    }
    hit() {
        if (this.IsRemove) return;
        AudioManager.PlaySound(Audios.HitDoor);
        Tween.stopAllByTarget(this.node);
        tween(this.node)
            .to(0.1, { scale: v3(1.1, 1.1, 1.1) })
            .to(0.1, { scale: v3(1, 1, 1) })
            .start();
        this.CurNum += this.AmpNum;
        this.CurNum = Number(this.CurNum.toFixed(this.Places))
        this.showUI();
    }

    onTriggerStay(event: ITriggerEvent) {
        if (event.otherCollider.getGroup() == Group.Bullet) {
            PoolManager.PutNode(event.otherCollider.node);
            this.hit();
        } else if (event.otherCollider.getGroup() == Group.Weapon) {
            if (this.IsAmplification) return;
            this.IsAmplification = true;
            if (this.AmplificationType == AMPLIFICATION.RATE) {
                GameManager.Instance.amplifiesRate(this.CurNum);
            } else if (this.AmplificationType == AMPLIFICATION.RANGE) {
                GameManager.Instance.amplifiesRange(this.CurNum);
            }
            this.removeSelf();
        }
    }

    removeSelf() {
        this.IsRemove = true;
        tween(this.node)
            .to(0.1, { scale: v3(1.1, 1.1, 1.1) }, { easing: `quadIn` })
            .to(0.1, { scale: v3(0, 0, 0) }, { easing: `quadOut` })
            .call(() => {
                this.node.destroy();
            })
            .start();
    }


}


